package com.xiaoq.social.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.social.entity.FansTagEntity;
import com.xiaoq.social.repository.IFansTagRepository;
import com.xiaoq.vo.PagingVo;

@Repository
public class FansTagRepositoryImpl implements IFansTagRepository {
	
	@Autowired
	private Datastore datasore;
	
	@Autowired
	private IdGenerator idGenerator;
	
	private static final Logger LOG = LoggerFactory.getLogger(FansTagRepositoryImpl.class);

	@Override
	public boolean addFansTag(FansTagEntity fansTag) {
		if(fansTag!=null){
			String id = idGenerator.nextId();
			fansTag.setId(id);
			LOG.info("插入的FansTagEntity实体记录的Id是"+id);
			datasore.save(fansTag);
			return true;
		}else{
			LOG.error("FansTagEntity 传入类型不能为空！");
		}
		return false;
	}

	@Override
	public boolean delFansTag(String userId, String tgtId) {
		Query<FansTagEntity> fansTagQuery = datasore.find(FansTagEntity.class);
		if(StringUtils.isNoneEmpty(userId)&&StringUtils.isNoneEmpty(tgtId)){
			fansTagQuery.field("tgtId").equal(tgtId).field("userId").equal(userId);
			if(fansTagQuery!=null){
				datasore.delete(fansTagQuery);
				return true;
			}
			
		}
		LOG.error("FansTag 删除失败！");
		return false;
	}

	@Override
	public List<FansTagEntity> fansTagList(String tgtId, List<String> tags, PagingVo pvo) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Long total = 0L;
		List<FansTagEntity> fansTagList = null;
		Query<FansTagEntity> fansTagQuery = datasore.find(FansTagEntity.class);
		if(StringUtils.isNotEmpty(tgtId)){
			fansTagQuery.field("tgtId").equal(tgtId);
			if(tags != null){
				fansTagQuery.field("tags").equal(tags);
			}
			if(fansTagQuery.count()!=0){
				total = fansTagQuery.count();
				pvo.setTc(total);
				fansTagQuery.order("createdAt").offset(pn).limit(ps);
				fansTagList = fansTagQuery.asList();
			}
		}else{
			LOG.error("必须传递关注对象的Id!");
		}
		return fansTagList;
	}

	@Override
	public boolean addTag(String tgtId, String userId, List<String> tags) {
		Query<FansTagEntity> fansTagQuery = datasore.find(FansTagEntity.class);
		if(StringUtils.isNotEmpty(tgtId)&&StringUtils.isNotEmpty(userId)){
			fansTagQuery.field("userId").equal(userId).field("tgtId").equal(tgtId);
			if(fansTagQuery.count()!=0){
				UpdateOperations<FansTagEntity> fansTagOperations = datasore.createUpdateOperations(FansTagEntity.class);
				if(tags!=null){
					LOG.info(tgtId+"给"+userId+"打"+tags+"标签");
					fansTagOperations.set("tags", tags);
					datasore.update(fansTagQuery, fansTagOperations);
					return true;
				}else{
					LOG.error("不能给用户打空标签！");
				}
			}else{
				LOG.debug("查无此粉丝，添加标签失败！");
			}
			
		}else{
			LOG.error("tgtId和userId都不能为空！");
		}
		return false;
	}

	@Override
	public Long findFansNum(String tgtId) {
		Query<FansTagEntity> fansTagQuery = datasore.find(FansTagEntity.class);
		if(StringUtils.isNotEmpty(tgtId)){
			fansTagQuery.field("tgtId").equal(tgtId);
			return fansTagQuery.count();
		}else{
			LOG.error("目标id不能为空！");
			return 0L;
		}
	}

}
